package work.timejay.shop.controller.user;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import work.timejay.common.ApiResult;
import work.timejay.dto.shop.UserLoginDTO;
import work.timejay.dto.shop.UserRegisterDTO;
import work.timejay.shop.entity.user.User;
import work.timejay.shop.service.user.UserService;
import work.timejay.utils.LoginHelper;
import work.timejay.vo.shop.UserLoginVO;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 王文杰
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    @SaIgnore
    public ApiResult<UserLoginVO> login(@RequestBody @Validated UserLoginDTO dto) {
        UserLoginVO token = userService.login(dto);
        return ApiResult.success(token);
    }

    @PostMapping(value = "/register")
    @SaIgnore
    public ApiResult<UserLoginVO> register(@RequestBody @Validated UserRegisterDTO dto) {
        UserLoginVO token = userService.register(dto);
        return ApiResult.success(token);
    }

    /**
     * 获取用户信息
     */
    @GetMapping(value = "/info")
    public ApiResult<JSONObject> getInfo() {
        JSONObject userJson = LoginHelper.loginUserInfoByJson();
        return ApiResult.success(userJson);
    }
}

